package com.hanserwei.community.manager;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@RequiredArgsConstructor
@Slf4j
public class RedisManager {

    private final RedisTemplate<String, String> redisTemplate;

    /**
     * 存入验证码
     */
    public void setCaptcha(String key, String value) {
        log.info("setCaptcha key={}, value={}", key, value);
        redisTemplate.opsForValue().set(key, value, 5, TimeUnit.MINUTES);
    }

    /**
     * 获取验证码
     */
    public String getCaptcha(String key) {
        log.info("getCaptcha key={}", key);
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 删除验证码
     */
    public void deleteCaptcha(String key) {
        log.info("deleteCaptcha key={}", key);
        redisTemplate.delete(key);
    }
}
